package com.emokili.ewa.portal.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.TypeReference;
import com.emokili.ewa.common.utils.Constant;
import com.emokili.ewa.common.utils.R;
import com.emokili.ewa.common.vo.MemberLoginVo;
import com.emokili.ewa.common.vo.MemberRegisterVo;
import com.emokili.ewa.common.vo.MemberUpdateVo;
import com.emokili.ewa.core.frame.entity.MemberEntity;
import com.emokili.ewa.core.frame.service.MemberService;
import com.emokili.ewa.core.frame.vo.LoginVo;
import com.emokili.ewa.portal.constant.Constants;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static com.emokili.ewa.portal.constant.Constants.LOGIN_USER;

/**
 * @author yuepanpan
 * @date 2021-05-29
 */
@Controller
@RequestMapping("/web")
public class LoginController {

    @Autowired
    MemberService memberService;


    @GetMapping(value = "/recaptcha")
    @ResponseBody
    public R recaptcha(@RequestParam("email") String email){
        MemberRegisterVo vo = new MemberRegisterVo();
        vo.setEmail(email);
        memberService.checkEmailUnique(vo);
        return R.ok();
    }

    @PostMapping("/register")
    @ResponseBody
    public R register(@RequestBody MemberRegisterVo vo) {
        memberService.register(vo);
        return R.ok();
    }

    @PostMapping("/login")
    @ResponseBody
    public R login(MemberLoginVo vo, RedirectAttributes attributes, HttpSession session){
        //远程登录
        MemberEntity login = memberService.login(vo);
        String token = UUID.randomUUID().toString();
        session.setAttribute(LOGIN_USER,login);
        return R.ok().put("token",token);
    }

    @PostMapping("/www-login-redirect")
    public String wwwLoginRedirect(LoginVo loginVo,HttpSession session){
        MemberEntity memberEntity = (MemberEntity) session.getAttribute(LOGIN_USER);
        return "redirect:http://127.0.0.1:8002";
    }

    @PostMapping("/update")
    @ResponseBody
    public R update(HttpServletRequest request, @RequestBody MemberUpdateVo memberUpdateVo){
        MemberEntity member = (MemberEntity) request.getAttribute("member");
        if(StringUtils.isNotEmpty(memberUpdateVo.getEmail())
                || StringUtils.isNotEmpty(memberUpdateVo.getCity())
                || StringUtils.isNotEmpty(memberUpdateVo.getFirstName())
                || StringUtils.isNotEmpty(memberUpdateVo.getLastName())
                || StringUtils.isNotEmpty(memberUpdateVo.getLanguage())
                || StringUtils.isNotEmpty(memberUpdateVo.getPassword())){
            memberUpdateVo.setId(member.getId());
            MemberEntity entity = memberService.update(memberUpdateVo);
            return R.ok().setData(entity);
        }else {
            return R.ok();
        }
    }

    @PostMapping("/logout")
    @ResponseBody
    public R logout(){
        return null;
    }
}
